* DiD OF RESIDENTS IN OW

* MASTER-DO-FILE, running all the do-files to produce the results of the stock

/* Results produced:
- Table 3: input needed for summary table (Columns 4-5)
- Figure 6a): Event Study graph
- Figure 7a): tax rate changes
- Table 4: elasticity estimates (Panel A) (results written to Excel)
- Table 5: heterogeneous elasticity estimates (Panel A) (results written to Excel)
*/

// NOTE: data needed for this do-file cannot be shared due to privacy reasons


version 16.1

clear all
cap log close 
cap clear matrix
set more off
cap set scheme mygraphs

local rescue_mypathRR "$mypathRR"

* get the data with all the indicators&dummies needed	
use "/Volumes/OW_data/my_dta_v8.1.dta", clear
label var zuzugjahre "move-in year (including several moves)"
label var zuzugjahr "move-in year (last move)"
drop if year>2010

bys year: egen residents=count(persid)

* Adjust taxable income to account for the changes in deduction in 2008
 // to account for the increased deduction under the flat-tax regime
replace esteuerbst=esteuerbst+10000 if year>=2008

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

cd "$mypathRR"

* GENERATE THE TREATMENT DEFINITION BASED ON RATE DETERMINING INCOME
gen taxinc_thre2 = (esatzbestkopf>= 300000 & esatzbestkopf <.)
label var taxinc_thre2 "Taxable income threshold regressive tax"

gen ctrl_ir_55_75 = (esatzbestkopf>= 300000*0.55 & esatzbestkopf< 300000*0.75)
label var ctrl_ir_55_75 "Control group: 55-75% of income threshold"

gen ctrl_ir_60_80 = (esatzbestkopf>= 300000*0.60 & esatzbestkopf< 300000*0.80)
label var ctrl_ir_60_80 "Control group: 60-80% of income threshold"

gen ctrl_ir_60_85 = (esatzbestkopf>= 300000*0.60 & esatzbestkopf< 300000*0.85)
label var ctrl_ir_60_85 "Control group: 60-85% of income threshold"

gen ctrl_ir_60_95 = (esatzbestkopf>= 300000*0.60 & esatzbestkopf< 300000*0.95)
label var ctrl_ir_60_95 "Control group: 60-95% of income threshold"

gen ctrl_ir_45_70 = (esatzbestkopf>= 300000*0.45 & esatzbestkopf< 300000*0.70)
label var ctrl_ir_45_70 "Control group: 40-70% of income threshold"

gen taxwea_thre = (vsatzbestkopf>= 5000000 & vsatzbestkopf <.)
label var taxwea_thre "Taxable wealth threshold regressive tax"

gen ctrl_wea = (vsatzbestkopf>= 3500000 & vsatzbestkopf< 4900000)
label var ctrl_wea "Control group: 3.5-4.9 million in taxable wealth"

gen treat_double = (taxwea_thre == taxinc_thre2 == 1)
label var treat_double "Treated by regressive wealth and income tax"

gen control_double = (ctrl_ir_60_80 == ctrl_wea == 1)
label var control_double "Control group: below income and wealth thresholds"


* Generate a dummy for each bracket
gen regbr1		= (esatzbestkopf>=300001 & esatzbestkopf<=340000)
gen regbr2		= (esatzbestkopf>=340001 & esatzbestkopf<=380000)
gen regbr3		= (esatzbestkopf>=380001 & esatzbestkopf<=420000)
gen regbr4		= (esatzbestkopf>=420001 & esatzbestkopf<=550000)
gen regbr5		= (esatzbestkopf>=550001 & esatzbestkopf<=600000)
gen regbr6		= (esatzbestkopf>=600001 & esatzbestkopf<=1000000)
gen regbr7		= (esatzbestkopf>=1000001 & esatzbestkopf<. )


* DEFINE WORKERS / COMMUTERS / RETIREES

* 1) Gen dummies for retirees
gen retiree01_1 = 1
foreach var in z100s1 z101s1 z104s1 z105s1 z110s1 z111s1 z114s1 z115s1 {
	replace retiree01_1 = 0 if `var' > 0 & `var' <.
}

gen retiree01_1dt  = 1
foreach var in z100s1  z104s1 z110s1  z114s1   {
	replace retiree01_1dt = 0 if `var' > 0 & `var' <.
}
gen retiree01_1ep = 1
foreach var in  z101s1  z105s1  z111s1  z115s1 {
	replace retiree01_1ep = 0 if `var' > 0 & `var' <.
}


gen retiree01_2 = 0
foreach var in z130s1 z131s1 z132s1 z133s1 z134s1 z135s1 {
	replace retiree01_2 = 1 if `var' > 0 & `var' <.
}

gen retiree01_2dt = 0
foreach var in z130s1  z132s1  z134s1  {
	replace retiree01_2dt = 1 if `var' > 0 & `var' <.
}
gen retiree01_2ep = 0
foreach var in  z131s1  z133s1 z135s1 {
	replace retiree01_2ep = 1 if `var' > 0 & `var' <.
}

gen retiree01_2sp = retiree01_2dt * retiree01_2ep

gen retiree01_3 = retiree01_2 * retiree01_1

label var retiree01_1 "Both partners are retirees: no labor income in couple"
label var retiree01_2 "At least one partner is retiree: some pension income in couple"
label var retiree01_2sp "Both partners are retirees: both have pension income each"
label var retiree01_3 "Retiree household with pension income and no labor income"

* 2) Gen dummies for working
gen inlf01_1 = (retiree01_1 ==0)
	label var inlf01_1 "Household has some labor income in couple"
gen inlf01_1dt = (retiree01_1dt ==0)
	label var inlf01_1dt "DT has some labor income"
gen inlf01_1ep = (retiree01_1ep ==0)
	label var inlf01_1ep "Spouse has some labor income"
	

* 3) Gen dummies for working outside of the canton
// ass 1: people who pay for annual transport pass commute out of canton
gen oev01_dt = (z201s1 > 1300 & z201s1 < .)
gen oev01_ep = (z202s1 > 1300 & z202s1 < .)
gen oev01 = (oev01_dt == 1 | oev01_ep == 1)

// ass 2: people who drive > 25km commute out of canton (distance Sarnen Lucerne)
/*  km x 2 x days of work per year == 222 x 0.7 CHF
	25 * 2 * 222 * 0.7 = 7770 CHF
	for spouse: if they work 50%, deduction will be 3885 CHF
*/
gen auto01_dt = (z205s1 > 7700 & z205s1 < .)
gen auto01_ep = (z206s1 > 3850 & z206s1 < .)
gen auto01 = (auto01_dt == 1 | auto01_ep == 1)

gen commuter01 = (auto01 == 1 | oev01 ==1 )

tab commuter01 inlf01_1



* DEFLATE THE INCOME VARIABLES
merge m:1 year using "$mypathRR/Datasets/CPI.dta"
drop _merge
drop if year==.

foreach var in esteuerbst vsteuerbst esatzbestkopf vsatzbestkopf steuern_eink steuern_verm z199s1 z450s1 z300s1 z470s1 inmob_inc mob_inc {
replace `var'=`var'/cpi*100.5 //deflate the series
local u : variable label `var'
local l= "`u' (real)" 
label var `var' "`l'"
}



* Generate the net-of-tax rate
gen avg_ntr=1-avgt_stek_taxable
label var avg_ntr "Average net-of-tax rate"
gen avg_ntr1=avg_ntr if year<2006 
	label var avg_ntr1 	"Avg. NTR $t<2006$"
gen avg_ntr2=avg_ntr if year>2005 & year<2008 
	label var avg_ntr2 	"Avg. NTR $t>=2006^*$"
	
	
	

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* SUMMARY STATISTICS
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
cd "$mypathRR/Resources"
do "Residents-summary_tables"

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* EVENT-STUDY GRAPHS OF DIFF-IN-DIFF
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
cd "$mypathRR/Resources"
do "Residents-ES_graphs.do"

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* DIFF-IN-DIFF STOCK OF RESIDENTS
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
cd "$mypathRR/Resources"
do "Residents-DiD.do"

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* DIFF-IN-DIFF SHARE OF RESIDENTS
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
cd "$mypathRR/Resources"
do "Residents-DiD_shares.do"

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* DESCRIPTIVES ABOUT COMMUTING AND WORKING STATUS OF RICH RESIDENTS
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
cd "$mypathRR/Resources"
do "Residents-commuters.do"


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * MAKE A NICE TABLE SUMMING UP ALL THE RESULTS * * * 
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

cd "$mypathRR/Results"

version 14.1
putexcel set "Tab4 - MainResults_formatted.xlsx", modify 

* reduced level (Col 1 = B)
putexcel B11 = "$DiD_60_80_B"
putexcel B12 = "($SE_60_80_B)"
putexcel B19 = $delta_tax_2006_60_80
putexcel B20 = $mean_incomers60_80

* reduced log (Col 2 = C)
putexcel C11 = "$DiD_60_80_C"
putexcel C12 = "($SE_60_80_C)"
putexcel C20 = $lnmean_incomers60_80

* 2SLS avg. tax rate, 2006-07 (Col 3 = D)
putexcel D13 = "$E_60_80_E"
putexcel D14 = "($SE_60_80_E)"
putexcel D15 = "$Ftest_60_80_st"

* 2SLS avg. tax rate, 2006-10 (Col 3 = D)
putexcel D16 = "${E10_60_80_E}"
putexcel D17 = "(${SE10_60_80_E})"
putexcel D18 = "$Ftest_60_80_st_2tr"

* reduced level (Col 4 = E)
putexcel E11 = "$DiD_60_95_B"
putexcel E12 = "($SE_60_95_B)"
putexcel E19 = $delta_tax_2006_60_95
putexcel E20 = $mean_incomers60_95

* reduced log (Col 5 = F)
putexcel F11 = "$DiD_60_95_C"
putexcel F12 = "($SE_60_95_C)"
putexcel F20 = $lnmean_incomers60_95

* 2SLS avg. tax rate, 2006-07 (Col 6 = G)
putexcel G13 = "$E_60_95_E"
putexcel G14 = "($SE_60_95_E)"
putexcel G15 = "$Ftest_60_95_st"

putexcel G16 = "$E10_60_95_E"
putexcel G17 = "($SE10_60_95_E)"
putexcel G18 = "$Ftest_60_95_st_2tr"

global drop _all

global mypathRR "`rescue_mypathRR'"


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* TABLE 5: HETEROGENEITY ANALYSIS 
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

version 16.1

* IN LABOR FORCE
cd "$mypathRR/Resources/"
do "ResidentsHeterog-in_LF.do"

* SELF EMPLOYED
cd "$mypathRR/Resources/"
do "ResidentsHeterog-selfemployed.do"

* COMMUTERS
cd "$mypathRR/Resources/"
do "ResidentsHeterog-commuters.do"

* RETIREES
cd "$mypathRR/Resources/"
do "ResidentsHeterog-retirees.do"



* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* SAVE RESULTS TO TABLE
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
cd "$mypathRR/Results"

version 14.1
putexcel set "Tab5 - Heterogeneity_formatted.xlsx", modify 

* PANEL A: IN LABOR FORCE
* reduced level (Col 1 = B)
putexcel B8 = "$DiD_60_80_BA"
putexcel B9 = "($SE_60_80_BA)"
putexcel B13 = $delta_tax_2006_60_80A
putexcel B14 = $mean_incomers60_80A

* reduced log (Col 2 = C)
putexcel C8 = "$DiD_60_80_CA"
putexcel C9 = "($SE_60_80_CA)"
putexcel C14 = $lnmean_incomers60_80A

* 2SLS avg. tax rate, 2006-10 (Col 3 = D)
putexcel D10 = "${E10_60_80_EA}"
putexcel D11 = "(${SE10_60_80_EA})"
putexcel D12 = "$Ftest_60_80_st_2trA"


* reduced level (Col 1 = B)
putexcel E8 = "$DiD_60_95_BA"
putexcel E9 = "($SE_60_95_BA)"
putexcel E13 = $delta_tax_2006_60_95A
putexcel E14 = $mean_incomers60_95A

* reduced log (Col 2 = C)
putexcel F8 = "$DiD_60_95_CA"
putexcel F9 = "($SE_60_95_CA)"
putexcel F14 = $lnmean_incomers60_95A

* 2SLS avg. tax rate, 2006-10 (Col 3 = D)
putexcel G10 = "${E10_60_95_EA}"
putexcel G11 = "(${SE10_60_95_EA})"
putexcel G12 = "$Ftest_60_95_st_2trA"




* PANEL B: SELF-EMPLOYED
* reduced level (Col 1 = B)
putexcel B18 = "$DiD_60_80_BB"
putexcel B19 = "($SE_60_80_BB)"
putexcel B23 = $delta_tax_2006_60_80B
putexcel B24 = $mean_incomers60_80B

* reduced log (Col 2 = C)
putexcel C18 = "$DiD_60_80_CB"
putexcel C19 = "($SE_60_80_CB)"
putexcel C24 = $lnmean_incomers60_80B

* 2SLS avg. tax rate, 2006-10 (Col 3 = D)
putexcel D20 = "${E10_60_80_EB}"
putexcel D21 = "(${SE10_60_80_EB})"
putexcel D22 = "$Ftest_60_80_st_2trB"


* reduced level (Col 1 = B)
putexcel E18 = "$DiD_60_95_BB"
putexcel E19 = "($SE_60_95_BB)"
putexcel E23 = $delta_tax_2006_60_95B
putexcel E24 = $mean_incomers60_95B

* reduced log (Col 2 = C)
putexcel F18 = "$DiD_60_95_CB"
putexcel F19 = "($SE_60_95_CB)"
putexcel F24 = $lnmean_incomers60_95B

* 2SLS avg. tax rate, 2006-10 (Col 3 = D)
putexcel G20 = "${E10_60_95_EB}"
putexcel G21 = "(${SE10_60_95_EB})"
putexcel G22 = "$Ftest_60_95_st_2trB"




* PANEL C: COMMUTERS
* reduced level (Col 1 = B)
putexcel B28 = "$DiD_60_80_BC"
putexcel B29 = "($SE_60_80_BC)"
putexcel B33 = $delta_tax_2006_60_80C
putexcel B34 = $mean_incomers60_80C

* reduced log (Col 2 = C)
putexcel C28 = "$DiD_60_80_CC"
putexcel C29 = "($SE_60_80_CC)"
putexcel C34 = $lnmean_incomers60_80C

* 2SLS avg. tax rate, 2006-10(Col 3 = D)
putexcel D30 = "${E10_60_80_EC}"
putexcel D31 = "(${SE10_60_80_EC})"
putexcel D32 = "$Ftest_60_80_st_2trC"


* reduced level (Col 1 = B)
putexcel E28 = "$DiD_60_95_BC"
putexcel E29 = "($SE_60_95_BC)"
putexcel E33 = $delta_tax_2006_60_95C
putexcel E34 = $mean_incomers60_95C

* reduced log (Col 2 = C)
putexcel F28 = "$DiD_60_95_CC"
putexcel F29 = "($SE_60_95_CC)"
putexcel F34 = $lnmean_incomers60_95C

* 2SLS avg. tax rate, 2006-10 (Col 3 = D)
putexcel G30 = "${E10_60_95_EC}"
putexcel G31 = "(${SE10_60_95_EC})"
putexcel G32 = "$Ftest_60_95_st_2trC"



* PANEL D: RETIREES
* reduced level (Col 1 = B)
putexcel B38 = "$DiD_60_80_BD"
putexcel B39 = "($SE_60_80_BD)"
putexcel B43 = $delta_tax_2006_60_80D
putexcel B44 = $mean_incomers60_80D

* reduced log (Col 2 = C)
putexcel C38 = "$DiD_60_80_CD"
putexcel C39 = "($SE_60_80_CD)"
putexcel C44 = $lnmean_incomers60_80D

* 2SLS avg. tax rate, 2006-10(Col 3 = D)
putexcel D40 = "${E10_60_80_ED}"
putexcel D41 = "(${SE10_60_80_ED})"
putexcel D42 = "$Ftest_60_80_st_2trD"


* reduced level (Col 1 = B)
putexcel E38 = "$DiD_60_95_BD"
putexcel E39 = "($SE_60_95_BD)"
putexcel E43 = $delta_tax_2006_60_95D
putexcel E44 = $mean_incomers60_95D

* reduced log (Col 2 = C)
putexcel F38 = "$DiD_60_95_CD"
putexcel F39 = "($SE_60_95_CD)"
putexcel F44 = $lnmean_incomers60_95D

* 2SLS avg. tax rate, 2006-10 (Col 3 = D)
putexcel G40 = "${E10_60_95_ED}"
putexcel G41 = "(${SE10_60_95_ED})"
putexcel G42 = "$Ftest_60_95_st_2trD"




global drop _all
clear frames
global mypathRR "`rescue_mypathRR'"


						* * * * *  E N D  * * * * * * 
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
